package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.ePlatformForbiddenType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eSpecialServerType;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.thirdparty.PlatformMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_CHANGE_NICK_NAME, desc = "修改昵称")
public class ChangeNickNameCmd extends AbstractCommand {
	private String nickName;
	private boolean isServerSend;
	private boolean needResp = false;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.ChangeNickNameReqMsg reqMsg = PlayerProto.ChangeNickNameReqMsg.parseFrom(packet.getBytes());
		nickName = reqMsg.getNickName();
		isServerSend = reqMsg.getIsServerSend();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!isServerSend && player.isRequestToFast()){
			needResp = true;
			ret  =  GameErrorCode.E_REQUEST_TOO_FAST;
			return;
		}

		if (ForbiddenMgr.isPlatformForbidden(player.getUserInfo().getPlatformUserId(), ePlatformForbiddenType.DISABLE_EDIT_NAME.getType())) {
			return;
		}

		ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
		if (serverInfo.getSpecialServer() == eSpecialServerType.Kowloon.getValue() && player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.CreateUserFreeChangeNickname) < 1) {

		}else {
			//编辑限制
			GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_CHANGE_NICK_NAME);
			if (null != gameEditLimit) {
				long nowTime = System.currentTimeMillis();
				if (nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()) {
					needResp = true;
					ret = GameErrorCode.E_SYSTEM_NO_OPEN;
					return;
				}
			}
		}
		/**
		 * 如果被禁用 则返回系统未开放
		 */
		if (ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_CHANGE_NICK_NAME)) {
			needResp = true;
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		
		String checkName = StringUtils.clearBlankSpace(nickName);
		if(StringUtils.isNullOrEmpty(checkName)){
			needResp = true;
			ret = GameErrorCode.E_FORBIDDEN_WORD;
			return;
		}

		int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
		if(!isServerSend && eChannelType.isMainLandChannel(channelId) && ChannelConfig.CN_FORBIDDEN_WORD_CHECK == 1) {//大陆渠道，实时检测角色昵称
			PlatformMgr.addChangeNickNameTask(player, nickName);
		}else {
			needResp = true;
			//盗墓笔记和九龙
			if ((serverInfo.getSpecialServer() == eSpecialServerType.Kowloon.getValue() || serverInfo.getSpecialServer() == eSpecialServerType.DaoMu.getValue()) && player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.CreateUserFreeChangeNickname) < 1) {
				// 特色服可以免费改名一次
				ret = PlatformMgr.changeNickName(player, nickName, false, false);
				if (ret == 0) {
					player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.CreateUserFreeChangeNickname, 1);
				}
			} else {
				ret = PlatformMgr.changeNickName(player, nickName, false, true);
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(needResp) {
			PlayerProto.ChangeNickNameRespMsg.Builder respMsg = PlayerProto.ChangeNickNameRespMsg.newBuilder();
			respMsg.setRet(ret);
			respMsg.setNickName(nickName);
			player.sendPacket(Protocol.U_CHANGE_NICK_NAME, respMsg);
		}
	}

}
